﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;



namespace WindowsGame
{
	public class ObjectRenderer
	{
		ObjectResources objectResources;

		IGameLogicService gameLogicService;

		GraphicsDevice graphicsDevice;



		public ObjectRenderer(GraphicsDevice graphicsDeviceIn, ObjectResources objectResourcesIn,
			IGameLogicService gameLogicServiceIn)
		{
			graphicsDevice = graphicsDeviceIn;
			objectResources = objectResourcesIn;
			gameLogicService = gameLogicServiceIn;
		}



		public void LoadContent()
		{
		}



		public void Draw(GameTime gameTime)
		{
			Debug.Assert(graphicsDevice != null);
			Debug.Assert(objectResources != null);
			Debug.Assert(gameLogicService != null);

			Matrix[] transforms = new Matrix[objectResources.model.Bones.Count];
			objectResources.model.CopyAbsoluteBoneTransformsTo(transforms);

			foreach(ModelMesh mesh in objectResources.model.Meshes)
			{
				foreach(BasicEffect effect in mesh.Effects)
				{
					effect.View = gameLogicService.GameState_.camera.viewMatrix.Value.GetNative();
					effect.Projection = gameLogicService.ProjectionMat;
					effect.World = transforms[mesh.ParentBone.Index] *
						gameLogicService.GameState_.player.rotationMat.Value.GetNative() *
						gameLogicService.GameState_.player.translationMat.Value.GetNative();
				}
				mesh.Draw();
			}
		}
	}
}